
#include "Triangle.h"
#include "Vertex.h"
#include <d3dx9math.h>

//
// Midpoints
//

Real3 Triangle::MidPoint(std::vector<Vertex> *vertices) const
{
	Real3 p = vertices->at(a).position + 
						vertices->at(b).position + 
						vertices->at(c).position;
	p *= (1.0f / 3.0f);

	return p;
}

Real3 Triangle::Side0MidPoint(std::vector<Vertex> *vertices) const
{
	return (vertices->at(a).position + vertices->at(b).position) * 0.5f;
}

Real3 Triangle::Side1MidPoint(std::vector<Vertex> *vertices) const
{
	return (vertices->at(c).position + vertices->at(b).position) * 0.5f;
}

Real3 Triangle::Side2MidPoint(std::vector<Vertex> *vertices) const
{
	return (vertices->at(a).position + vertices->at(c).position) * 0.5f;
}

//
// Normals
//

Real3 Triangle::Normal(std::vector<Vertex> *vertices) const
{
	Real3 normal;
	Real3 u = NormalOf(vertices->at(b).position - vertices->at(a).position);
	Real3 v = NormalOf(vertices->at(c).position - vertices->at(a).position);
	D3DXVec3Cross((D3DXVECTOR3*)&normal, (D3DXVECTOR3*)&u, (D3DXVECTOR3*)&v); 

	return normal;

	//return Cross(NormalOf(vertices->at(b).position - vertices->at(a).position),
	//						 NormalOf(vertices->at(c).position - vertices->at(a).position));
}

Real3 Triangle::Side0Normal(std::vector<Vertex> *vertices) const
{
	return (vertices->at(a).normal + vertices->at(b).normal) * 0.5f;
}

Real3 Triangle::Side1Normal(std::vector<Vertex> *vertices) const
{
	return (vertices->at(c).normal + vertices->at(b).normal) * 0.5f;
}

Real3 Triangle::Side2Normal(std::vector<Vertex> *vertices) const
{
	return (vertices->at(a).normal + vertices->at(c).normal) * 0.5f;
}

//
// Texture Coordinates
//

Real2 Triangle::Tex(std::vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	Real2 t = vertices->at(a).tex + 
						vertices->at(b).tex + 
						vertices->at(c).tex;
	t *= (1.0f / 3.0f);

	return t;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Triangle::Side0Tex(std::vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(a).tex + vertices->at(b).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Triangle::Side1Tex(std::vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(c).tex + vertices->at(b).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}

Real2 Triangle::Side2Tex(std::vector<Vertex> *vertices) const
{
	#ifdef USE_TEX
	return (vertices->at(a).tex + vertices->at(c).tex) * 0.5f;
	#else
	return Real2(0.0f, 0.0f);
	#endif
}
